// pages/order/index.js
import {request,fail,ok} from "../../util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history: [],
    historyShow: false,
    keyword: undefined,
    navActive: 0,
    navArr: [{
      statusName: "全部",
      status: null
    }, {
      statusName: "待支付",
      status: 0
    }, {
      statusName: "待发货",
      status: 1
    }, {
      statusName: "待收货",
      status: 2
    }, {
      statusName: "待评价",
      status: 3
    }
  ],
  orderList:[]
  },
  navChange(e){
    //console.log(e.detail);
    this.setData({navActive:e.detail.index})
    this.loadOrder()
  },
  onChange(e) {
    this.setData({
      keyword: e.detail
    })
  },
  onFocus(e) {
    this.setData({
      historyShow: true
    })
  },
  cleanHistory() {
    this.setData({
      history: []
    })
  },
  clickHistoryItem(e) {
    let item = e.currentTarget.dataset.id
    this.setData({
      keyword: item
    })
    //搜索逻辑
    this.onSearch()
  },
  onSearch() {
    if (this.data.keyword && this.data.keyword.length > 0) {
      let history = this.data.history
      history.unshift(this.data.keyword)
      history = [...new Set(history)]
      this.setData({
        history: history
      })
      wx.setStorageSync('orderHistory', history)
    }

    this.setData({
      historyShow: false
    })
    this.loadOrder()
  },
  loadOrder(){
    let status= this.data.navArr[this.data.navActive].status
    status= status==null?"":status
    let name= this.data.keyword?this.data.keyword:""

    request({
      url:"/order/list?pageShow=false&status="+status+"&name="+name,
      method:"GET"
    }).then(res=>{
      this.setData({orderList:res.data.list})
    })
  },
  cancelOrder(e){
    let orderNo= e.currentTarget.dataset.id
    request({
      url:"/order/cancal?orderNo="+orderNo,
      method:"POST"
    }).then(res=>{
      this.loadOrder()
    })
  },
  reOrder(e){
    let orderNo= e.currentTarget.dataset.id
    request({
      url:"/order/reorder?orderNo="+orderNo,
      method:"POST"
    }).then(res=>{
      wx.navigateTo({
        url: '/pages/prepay/index?cartIds='+ res.data.join(","),
      })
    })
  },

  startPay(e){
    let orderNo= e.currentTarget.dataset.id
    request({
      url:"/pay/request",
      method:"POST",
      data:{
        orderNo: orderNo,
        channel: 0
      }
    }).then(res2=>{
      wx.requestPayment(
        {
          timeStamp: res2.data.timeStamp,
          nonceStr: res2.data.nonceStr,
          package: res2.data.package,
          signType: res2.data.signType,
          paySign: res2.data.paySign,
          success :res3=> {
            request({
              url:"/pay/query?orderNo="+res.data
            }).then((res5)=>{
                ok("订单支付成功")
                this.loadOrder()
            })
           },
          fail :res4=> {
            //console.log(res);
            fail("订单未支付")
            this.loadOrder()
           }
        }
      )
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let status=options.status
    this.setData({navActive:parseInt(status)})
    this.loadOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let history = wx.getStorageSync('orderHistory') || []
    this.setData({
      history
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})